<template>
  <!-- 应付管理列表页 -->
  <div>
    <div class="shareContainer" ref="shareContainer">
      <!-- <el-tabs
        class="tab"
        type="border-card"
        v-model="activeName"
        @tab-click="displayOrders()"
      >
        <el-tab-pane label="待付款" name="0"> </el-tab-pane>
        <el-tab-pane label="已核销" name="1"> </el-tab-pane>
      </el-tabs> -->
      <div class="content-wrapper">
        <div class="searchContainer_button">
          <div style="margin-bottom: 10px">
            <el-button type="primary" plain @click="handleClickImport" v-if="activeName == 0">导入</el-button>
            <el-button type="primary" plain @click="exportData">导出</el-button>
            <el-button type="primary" plain @click="editSeries" v-if="activeName == 0">付款</el-button>
          </div>
          <div>
            <div style="display: flex">
              <el-button type="primary" plain @click="resultClickFn" style="margin: 0px 10px 0px 0px">重置</el-button>
              <SettinUp :settype="22" :objSettinUpdata="objSettinUp" key="0"></SettinUp>
            </div>
          </div>
        </div>
        <div>
          <div v-if="header_data_list.length > 0">
            <TableHeader ref="TableHeaderList" :header_data_list="header_data_list" :HeaderData="HeaderData"
              :propMap="propMap" :objheader="objheader" @childEvent="handleDelete" @orderIdNumsEvent="handleDeleteType"
              :operation_type="operation_type" @handleClickDetail_second="handleClickDetail_second"
              @handleClickEdit_second="handleClickEdit_second" particulars="finance_payment_list"></TableHeader>
          </div>
          <div class="operation" style="margin: 10px 0px 0px 0px">
            <el-pagination background @size-change="handleSizeChange2" @current-change="handleCurrentChange2"
              :current-page="queryData.page" :page-sizes="[10, 20, 30, 40, 50]" :page-size="20"
              layout="total, sizes, prev, pager, next, jumper" :total="totalListNum">
            </el-pagination>
          </div>
        </div>
        <div class="dialog_unpdata">
          <!-- <el-dialog title="修改内容" :visible.sync="dialogVisible" width="40%">
           
          </el-dialog> -->
        </div>
        <!-- 详情数据 -->
        <div class="dialog_unpdata">
          <el-dialog title="付款详情" center :visible.sync="dialogVisible" width="70%">
            <el-form :model="formData" :inline="true" label-width="100px" label-position="top"
              style="margin: 0px 0px 0px 10px">
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item label="原因:">
                    <span style="font-weight: bold;color: #F09937;">{{ formData.reason }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="申请单号:">
                    <span class="paymentstyle">{{ formData.source_no }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="来源:">
                    <span class="paymentstyle">{{ formData.source_type | afterTime }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="所属组织:">
                    <span class="paymentstyle">{{ formData.branch_id_name }}</span>
                  </el-form-item>
                </el-col>
                <!-- <el-col :span="6">
                  <el-form-item label="状态:">
                    <span>{{formData.status | statusList }}</span>
                  </el-form-item>
                </el-col> -->
                <el-col :span="6">
                  <el-form-item label="收款人:">
                    <span class="paymentstyle">{{ formData.payee }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="收款银行:">
                    <span class="paymentstyle">{{ formData.receive_bank }}</span>
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="应付金额:">
                    <span class="paymentstyle">{{ formData.payable_money }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="已付金额:">
                    <span class="paymentstyle">{{ formData.payment_money }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item label="收款银行账号:">
                    <!-- <el-input type="textarea" :rows="1" placeholder="请输入内容" v-model="formData.receive_bank_account">
                    </el-input> -->
                    <span class="paymentstyle">{{ formData.receive_bank_account }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <el-form :model="payUpdate" :inline="true" label-width="80px" label-position="right" ref="ruleForm"
              :rules="payUpdateRule" style="margin: 0px 0px 0px 10px">
              <el-row :gutter="20">
                <el-col :span="8"><el-form-item label="支付金额" prop="payment_money">
                    <el-input v-model="payUpdate.payment_money" type="Number"></el-input> </el-form-item></el-col>
                    <el-col :span="8"><el-form-item label="调整金额" prop="adjust_money">
                      <el-input v-model="payUpdate.adjust_money" type="Number"></el-input> </el-form-item></el-col>
                <el-col :span="8">
                  <el-form-item label="付款主体" prop="payment_account_id">
                    <el-select v-model="payUpdate.payment_account_id" placeholder="请选择">
                      <el-option v-for="item in accountNumber" :key="item.id" :label="item.invoice_body_name"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="备注" prop="remark">
                    <el-input v-model="payUpdate.remark" type="textarea" :rows="1"></el-input> </el-form-item></el-col>
              </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button @click="derive_export(formData)">导 出</el-button>
              <el-button type="primary" @click="paymentUpdateObj('ruleForm')">确 定</el-button>
            </span>
          </el-dialog>
        </div>
        <div>
          <ImportRules @import="handleImport" :downloadUrl="downloadUrl" :drawerfeesData="importDialogShow"
            @drawerfeesShow="drawerfeesFlagShowFn" />
        </div>
        <!-- 详情数据内容 -->
        <div class="dialog_payment">
          <el-dialog title="付款详情" :visible.sync="dialogVisiblepayment" width="70%" center >
            <el-form :model="formData" label-width="80px" label-position="top" style="margin: 0px 0px 0px 10px">
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item label="原因:">
                    <span class="paymentstyle">{{ formData.reason }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="申请单号:">
                    <span style="color: #409eff; cursor: pointer;" class="fontBoldstyle" @click="
                        requestCode(
                          formData.source_id,
                          formData.examine_record_id,
                          formData.source_type
                        )
                      ">{{ formData.source_no }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="来源:" label-width="50px">
                    <span class="paymentstyle">{{ formData.source_type | afterTime }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="所属组织:">
                    <span class="paymentstyle">{{ formData.branch_id_name }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="状态:" label-width="50px">
                    <span class="paymentstyle">{{ formData.status | statusList }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="收款人:">
                    <span class="paymentstyle">{{ formData.payee }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="收款银行:">
                    <span class="paymentstyle">{{ formData.receive_bank }}</span>
                  </el-form-item>
                </el-col>

                <el-col :span="6">
                  <el-form-item label="应付金额:">
                    <span class="paymentstyle">{{ formData.payable_money }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="已付金额:">
                    <span class="paymentstyle">{{ formData.payment_money }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="调整金额:">
                    <span class="paymentstyle">{{ formData.adjust_money }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="收款银行账号:" label-width="100px">
                    <span class="paymentstyle">{{ formData.receive_bank_account }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="付款主体:">
                    <span class="paymentstyle" v-if="
                        dialogVisiblepayment == true &&
                        formData.payment_account_id_info != undefined
                      ">{{
                      formData.payment_account_id_info.invoice_body_name
                      }}</span>

                    <!-- <span>{{Object.keys(formData).length != 0 ? formData.payment_account_id_info
                      :""}}</span> -->
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="derive_export(formData)">导 出</el-button>
              <el-button @click="dialogVisiblepayment = false">取 消</el-button>
            </span>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { downloadFile } from "@/page/material/common/common";
  import SettinUp from "@/page/orderform/components/settingUp/settingUp.vue";
  import TableHeader from "@/page/orderform/components/TableHeader/tableheader.vue";
  import { timestamp10 } from "@/utils/mUtils";
  import ImportRules from "@/page/companymanagement/components/importRules.vue";
  import {
    finance_payment_list,
    finance_payment_update,
    finance_invoice_book_box,
    finance_payment_detail,
    finance_payment_import_update,
    finance_payment_export,
    payable_apply_export,
    preimburse_apply_export,
    petty_cash_apply_export,
    payment_apply_export,
  } from "@/api/paymentrequest";
  export default {
    name: "paymentManage",
    components: { TableHeader, SettinUp, ImportRules },
    filters: {
      afterTime(value) {
        switch (value) {
          case "0":
            return "其它";
          case "1":
            return "承运商应付申请";
          case "2":
            return "报销申请";
          case "3":
            return "备用金申请";
          case "4":
            return "其他付款申请";
          case "5":
            return "发票申请";
          case "6":
            return "其他付款申请";
          case "7":
            return "采购申请";
          case "8":
            return "中通充值申请";
        }
      },
      statusList(value) {
        switch (value) {
          case "0":
            return "待付款";
          case "1":
            return "已付款";
        }
      },
    },
    data() {
      return {
        queryData: {
          header_id: "22",
          page: 1,
          page_size: 20,
        },
        totalListNum: 0,
        activeName: 0,
        header_data_list: [],
        propMap: {},
        obj_list: {},
        HeaderData: [],
        waybillIdNums: [],
        operation_type: 1,
        dialogVisible: false,
        dialogVisiblepayment: false,
        payUpdate: {
          id: "",
          payment_money: "",
          payment_account_id: "",
          adjust_money:"",// 调整金额
          remark: "",
        },
        payUpdateRule: {
          payment_money: [
            { required: true, message: "请输入支付金额", trigger: "blur" },
          ],
          payment_account_id: [
            { required: true, message: "请输入支付主体", trigger: "blur" },
          ],
          adjust_money: [
            { required: true, message: "请输入调整金额", trigger: "blur" },
          ],
          remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
        },
        accountNumber: [],
        formData: {},
        importDialogShow: false,
        downloadUrl:
          "https://dus-company-log-img.oss-cn-beijing.aliyuncs.com/6/1685361603575%E5%BA%94%E4%BB%98%E8%B4%A6%E6%AC%BE%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx",
      };
    },
    mounted() {
      this.financeList();
      // 账号申请
      this.financeInvoiceObj();
    },
    computed: {},
    watch: {
      $route: {
        handler(newV, oldV) {
          if (this.$route.query.type == 0) {
            this.activeName = "0";
            this.displayOrders();
          } else if (this.$route.query.type == 1) {
            this.activeName = "1";
            this.displayOrders();
          }
          // console.log(this.$route.query.type, "this.$route.query.type");
        },
        immediate: true,
        deep: true,
      },
    },
    methods: {
      displayOrders(val) {
        this.waybillIdNums = [];
        this.obj_list = {};
        this.financeList();
        setTimeout(() => {
          this.resultClickFn();
        }, 800);
      },
      objSettinUp() {
        this.CapacityList();
      },
      financeList() {
        var param = {
          ...this.queryData,
          status: this.activeName + "",
          ...this.obj_list,
        };
        finance_payment_list(param).then((res) => {
          if (res.code == 10000) {
            this.orders = res.data.data || [];
            this.orders.unshift({});
            this.header_data_list = this.orders;
            this.HeaderData = res.data.item_list;
            this.Fntransformation(this.HeaderData);
            this.totalListNum = res.data.pagination.total;
          }
        });
      },
      objheader(obj) {
        // this.queryData.page_size = 10
        // this.queryData.page = 1
        var data = {
          ...obj,
          status: this.activeName + "",
          header_id: 22 + "",
          page: this.queryData.page,
          page_size: this.queryData.page_size,
        };
        this.obj_list = obj;
        finance_payment_list(data).then((res) => {
          this.header_data_list = res.data.data;
          if (res.code === 10000 && res.data.data !== null) {
            this.header_data_list.unshift({});
            this.totalListNum = res.data.pagination.total;
          } else {
            this.header_data_list = [{}];
            this.paginations.total = 0;
            this.orderslength = 0;
            // this.queryData.page_size = 10
            // this.queryData.page = 1
          }
        });
      },
      handleDeleteType(value) {
        // console.log(value, "valuefjalsjdf");
        this.waybillIdNums = value;
      },
      objSettinUp() {
        this.CapacityList();
      },
      Fntransformation(obj) {
        const result = {};
        for (const prop in obj) {
          if (obj.hasOwnProperty(prop)) {
            if (obj[prop].send_name !== "") {
              result[obj[prop].name] = obj[prop].show_name;
            } else {
              result[obj[prop].name] = obj[prop].name;
            }
          }
        }
        this.propMap = result;
      },
      handleSizeChange2(val) {
        this.queryData.page_size = val;
        this.financeList();
      },
      handleCurrentChange2(val) {
        this.queryData.page = val;
        this.financeList();
      },
      resultClickFn() {
        this.$nextTick(() => {
          this.$refs.TableHeaderList.childMethod();
        });
        this.page_size = 20;
        this.page = 1;
      },
      handleClickDetail_Type(val) {
        var param = {
          id: val + "",
        };
        finance_payment_detail(param).then((res) => {
          this.formData = res.data;
          this.payUpdate.payment_money = res.data.payable_money;
          this.payUpdate.adjust_money = res.data.payable_money;
          if (res.data.payment_account_id_info)
            this.payUpdate.payment_account_id =
              res.data.payment_account_id_info.invoice_body_id;
          // console.log("fhaksjdfhk");
        });
      },
      handleClickDetail_second(val) {
        this.dialogVisiblepayment = true;
        var param = {
          id: val.id,
        };
        finance_payment_detail(param).then((res) => {
          this.formData = res.data;
        });
        // console.log(val, "handleClickEdit_second");
      },
      handleClickEdit_second(val) { },
      handleDelete() { },
      //控制导入是否显示函数
      drawerFlagShowFn(data) {
        this.drawerFlagShow = data;
      },
      // 编辑接口
      editSeries() {
        if (this.waybillIdNums.length == 1) {
          this.dialogVisible = true;

          this.handleClickDetail_Type(this.waybillIdNums[0]);
        } else {
          this.open2("只能选择一条数据", "warning");
        }
      },
      // 账号接口
      financeInvoiceObj() {
        var param = {
          // 主体名
          invoice_body_name: "",
          invoice_body_belong: 0 + "", // 发票主体所属：0公司，1客户，2承运商
        };
        finance_invoice_book_box(param).then((res) => {
          this.accountNumber = res.data || [];
          // console.log(this.accountNumber, "accountNumber申请账号");
        });
      },
      // 修改数据
      paymentUpdateObj(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.payUpdate.adjust_money <= this.formData.payable_money) {
              var param = {
              ...this.payUpdate,
              id: this.waybillIdNums[0] + "",
            };
            finance_payment_update(param).then((res) => {
              // console.log(res, "修改数据成功");
              if (res.code == "10000") {
                this.open2("付款成功", "success");
                this.dialogVisible = false;
                Object.assign(
                  this.$data.payUpdate,
                  this.$options.data().payUpdate
                );
                this.financeList();
              }
            });
            }else{
              this.open2("调整金额必须小于等于应付金额", "warning");
            }
        
          } else {
            // console.log("error submit!!");
            return false;
          }
        });
      },
      //导入函数
      drawerfeesFlagShowFn(data) {
        this.importDialogShow = data;
      },
      handleClickImport() {
        this.importDialogShow = true;
      },
      // 点击弹框提交导入
      handleImport(filename, cb) {
        finance_payment_import_update({
          file_name: filename,
          header_id: "22",
        }).then((res) => {
          if (res.code === 10000) {
            cb(res || {});
            this.getDetail();
          }
        });
      },
      // 导出数据
      exportData() {
        var param = {
          ...this.obj_list,
          status: this.activeName,
          create_time: "",
          ids: this.waybillIdNums.map(Number),
          header_id: 22 + "",
        };
        finance_payment_export(param).then((res) => {
          this.reqUrl = "https://" + res.data.loaded_url;
          window.open(this.reqUrl);
        });
      },
      open2(message, type) {
        this.$message({
          message: message,
          type: type,
        });
      },
      requestCode(source_id, examine_record_id, value) {
      console.log(source_id,"source_id")
      console.log(examine_record_id,"source_id")
      console.log(value,"value")
        // audit_id:124
        // id ：42
        if (value == 1) {
          this.$router.push({
            name: "thecarrierPaysadd",
            query: {
              audit_id: examine_record_id,
              id: source_id,
              Type: 2,
              TageType: 5,
            },
          });
        } else if (value == 2) {
          this.$router.push({
            name: "reimburSementadd",
            query: {
              Type: 2,
              audit_id: examine_record_id,
              id: source_id,
              TageType: 5,
            },
          });
        } else if (value == 3) {
          // type 这个是 判断是 查看还是编辑 id 当行传递的 id Tage 表示的是类型

          this.$router.push({
            name: "reserveMoneyadd",
            query: {
              Type: 2,
              TageType: 5,
              audit_id: examine_record_id,
              id: source_id,
            },
          });
        } else if (value == 4) {
          this.$router.push({
            name: "paymentReqAdd",
            query: {
              Type: 2,
              TageType: 5,
              audit_id: examine_record_id,
              id: source_id,
            },
          });
        }else if (value == 5) {
          this.$router.push({
            name: "receiptManageadd",
            query: {
              Type: 2,
              TageType: 5,
              audit_id: examine_record_id,
              id: source_id,
            },
          });
        } else if (value == 6) {
          this.$router.push({
            name: "useStampsAdd",
            query: {
              Type: 2,
              TageType: 5,
              audit_id: examine_record_id,
              id: source_id,
            },
          });
        }else if (value == 7) {
          this.$router.push({
            name: "purChaseadd",
            query: {
              Type: 2,
              TageType: 5,
              audit_id: examine_record_id,
              id: source_id,
            },
          });
        }
        else if (value == 8) {
          this.$router.push({
            name: "ztoRechargeAdd",
            query: {
              Type: 2,
              TageType: 5,
              audit_id: examine_record_id,
              id: source_id,
            },
          });
        }
      },
      // 导出
      derive_export(value) {
        if (value.source_type == 1) {
          var param = {
            id: value.source_id,
          };
          payable_apply_export(param).then((res) => {
            if (res.code == 10000) {
              downloadFile(res.data.loaded_url);
              this.$message({
                type: "success",
                message: "导出成功",
              });
            }
          });
        } else if (value.source_type == 2) {
          var param = {
            id: value.source_id,
          };
          preimburse_apply_export(param).then((res) => {
            if (res.code == 10000) {
              downloadFile(res.data.loaded_url);
              this.$message({
                type: "success",
                message: "导出成功",
              });
            }
          });
        } else if (value.source_type == 3) {
          var param = {
            id: value.source_id,
          };
          petty_cash_apply_export(param).then((res) => {
            if (res.code == 10000) {
              downloadFile(res.data.loaded_url);
              this.$message({
                type: "success",
                message: "导出成功",
              });
            }
          });
        } else if (value.source_type == 4) {
          var param = {
            id: value.source_id,
          };
          payment_apply_export(param).then((res) => {
            if (res.code == 10000) {
              downloadFile(res.data.loaded_url);
              this.$message({
                type: "success",
                message: "导出成功",
              });
            }
          });
        }
      },
    },
  };
</script>

<style lang="less" scoped>
  // /deep/ .el-tabs__nav-wrap {
  //   left: -178px;
  // }
  .shareContainer {
    padding: 10px 15px;
    background-color: #fff;

    .searchContainer_button {
      display: flex;
      justify-content: space-between;
    }

    .el-form-item--mini.el-form-item {
      margin-bottom: 10px;
    }

    .operation {
      height: 40px;

      .el-button {
        margin-left: 0px;
        margin-right: 10px;
      }

      ::v-deep .el-pagination {
        float: right;
        margin: 0px 0px 0px 20px;

        .el-input--mini .el-input__inner {
          height: 24px;
        }
      }
    }

    /deep/ .el-tabs--border-card>.el-tabs__content {
      padding: 0px;
    }

    .tab.el-tabs--border-card {
      border: none;
      box-shadow: none;

      .el-tabs__content {
        padding: 0;
        height: 40px;
      }
    }

    .fontBoldstyle {
      font-weight: bold;
    }
    .paymentstyle{
      font-weight: bold;
      color: #F09937;
    }

  }
</style>